package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.service.CourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author AIR
 * @since 2022-09-20
 */
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin
public class CourseController {

    @Autowired
    private CourseService courseService;

    //课程基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回添加之后课程id，为了后面添加大纲使用
            String id = courseService.saveCourseInfo(courseInfoVo);
            return R.ok().data("courseId",id);

    }

    //根据课程id查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    @CacheEvict(value = "course",allEntries = true)
    //修改课程信息
    @PutMapping("updateCourseInfo/{courseId}")
    public R updateCourseInfo(@PathVariable("courseId") String courseId,@RequestBody CourseInfoVo courseInfoVo){
        System.out.println(courseInfoVo);
        courseService.updateCourseInfo(courseId,courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable("courseId") String courseId){
        CoursePublishVo coursePublishVo = courseService.getPublishCourse(courseId);
        return R.ok().data("getPublishCourse",coursePublishVo);
    }

    //课程最终发布
    //修改课程状态
    @PutMapping("publishCourse/{courseId}")
    public R publishCourse(@PathVariable("courseId") String courseId){
        courseService.updateStatusById(courseId);
        return R.ok();
    }

    //查询的课程列表
    @PostMapping("coursePageQuery/{page}/{limit}")
    public R getAllPublishCourse(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 @RequestBody(required = false) CourseQuery courseQuery){
        Page<EduCourse> pageParam = new Page<>(page,limit);

        courseService.getPublishCourseList(pageParam,courseQuery);
        List<EduCourse> eduCourses = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("items",eduCourses).data("total",total);
    }

    @CacheEvict(value = "course",allEntries = true)
    //根据id删除课程列表
    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable("courseId") String courseId){
        courseService.removeCourse(courseId);
        return R.ok();
    }
}
